寫這篇主要是提供那些動不動會改錯料號
需要回復前一版料號,首先要知道如果關聯是用float時
子階料號單獨變更,被連結的其他物件也會跟著更新
所以要回復前一版,其他的連結也要一併更新,風險很大評估後再來修正
正常功能在編輯->清除Purge
以下程式用在DCO用執行按鈕,回復受影響物件
Aras.Server.Security.Identity plmIdentity = Aras.Server.Security.Identity.GetByName("Innovator Admin");
Boolean PermissionWasSet = Aras.Server.Security.Permissions.GrantIdentity(plmIdentity);
Innovator inn = this.getInnovator();
string form_id = this.getProperty("id");
string aml = @"<AML>
<Item action='get' type='Express DCO Affected Item' select='source_id,related_id(affected_id,new_item_id)'>
<source_id>
<Item type='Express DCO' action='get' select='item_number'>
<id>{0}</id>
</Item>
</source_id>
</Item>
</AML>";
aml = string.Format(aml,form_id);
Item itms = inn.applyAML(aml);
if(itms.isError()==false){
for(int i=0;i<itms.getItemCount();i++){
Item itm = itms.getItemByIndex(i);
Item relatedItem = itm.getPropertyItem("related_id");
string affect_id = relatedItem.getProperty("affected_id","");
string new_item_id = relatedItem.getProperty("new_item_id","");
relatedItem.setProperty("new_item_id","");
Item result = relatedItem.apply("edit");
if(affect_id!=""){
string affect_type = "Part";
Item controlItem = inn.getItemById(affect_type,affect_id);
if(controlItem!=null){
if(controlItem.getProperty("state")=="In Change"){
controlItem.promote("Released","");
}
}
string bulkchangeAML = @"<AML>
<Item action='get' type='Bulk Change'>
<parent_partno>
<Item type='Part' action='get'>
<id>{0}</id>
</Item>
</parent_partno>
</Item>
</AML>";
bulkchangeAML = string.Format(bulkchangeAML,new_item_id);
Item bulkItems = inn.applyAML(bulkchangeAML);
if(bulkItems.isError()==false){
List<string> delItems = new List<string>();
for(int row=0;row < bulkItems.getItemCount();row++){
Item itmB = bulkItems.getItemByIndex(row);
delItems.Add(itmB.getID());
}
foreach(string id in delItems){
string delAML = @"<AML>
<Item action='delete' type='Bulk Change' id='{0}'>
</Item>
</AML>";
delAML = string.Format(delAML,id);
Item itmD = inn.applyAML(delAML);
}
}
string bom_AML = @"";
Item new_part = inn.newItem("part","get");
new_part.setProperty("id",new_item_id);
new_part = new_part.apply();
//找最後變更前的版本
string sqlFindOldRevPart = @"select id
from
(
select id,item_number,MAJOR_REV,GENERATION,state from innovator.part
where config_id = '{0}'
and is_current='0'
and (
select MAX(MAJOR_REV)
from innovator.part
where config_id = '{0}'
and is_current='0'
) = MAJOR_REV
) as t
where t.GENERATION = (
select Max(GENERATION) from innovator.part
where config_id = '{0}'
and is_current='0'
and (
select MAX(MAJOR_REV)
from innovator.part
where config_id = '{0}'
and is_current='0'
) = MAJOR_REV
)";
sqlFindOldRevPart = string.Format(sqlFindOldRevPart,new_part.getProperty("config_id",""));
Item oldPart = inn.applySQL(sqlFindOldRevPart);
//return inn.newError(oldPart.ToString());
if(oldPart.isError()==false){
string oldPartId = oldPart.getProperty("id","");
Item boms = inn.newItem("Part BOM","get");
boms.setProperty("related_id",new_item_id);
boms = boms.apply();
if(boms.isError()==false){
for(int k=0;k<boms.getItemCount();k++){
Item bom = boms.getItemByIndex(k);
string bom_id = bom.getProperty("id");
string sqlUpdate = @"update innovator.part_bom set RELATED_ID = '{0}' where part_bom.id='{1}'";
sqlUpdate = string.Format(sqlUpdate,oldPartId,bom_id);
inn.applySQL(sqlUpdate);
}
}
Item altBom = inn.newItem("BOM Substitute","get");
altBom.setProperty("related_id",new_item_id);
altBom = altBom.apply();
if(altBom.isError()==false){
for(int k=0;k<altBom.getItemCount();k++){
Item abom = altBom.getItemByIndex(k);
string bom_id = abom.getProperty("id");
string sqlUpdate = @"update innovator.bom_substitute set RELATED_ID = '{0}' where bom_substitute.id='{1}'";
sqlUpdate = string.Format(sqlUpdate,oldPartId,bom_id);
inn.applySQL(sqlUpdate);
}
}
string affAML = @"<AML>
<Item action='get' type='Affected Item'>
<affected_id>{0}</affected_id>
</Item>
</AML>";
affAML = string.Format(affAML,new_item_id);
Item affItems = inn.applyAML(affAML);
if(affItems.isError()==false){
for(int a=0;a<affItems.getItemCount();a++){
Item affItem = affItems.getItemByIndex(a);
string updateAffSQL = @"Update innovator.Affected_Item set affected_id='{0}' where id='{1}'";
updateAffSQL = string.Format(updateAffSQL,oldPartId,affItem.getID());
inn.applySQL(updateAffSQL);
}
}
}
string purgeAML = @"<AML>
<Item action='purge' type='Part' id='{0}'>
</Item>
</AML>";
purgeAML = string.Format(purgeAML,new_item_id);
Item purgeItem = inn.applyAML(purgeAML);
if(purgeItem.isError()){
return inn.newError("AML="+purgeAML);
}
}
}
}
if (PermissionWasSet) Aras.Server.Security.Permissions.RevokeIdentity(plmIdentity);
return this;